﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System;

public class YIjieDanjiManager : MonoBehaviour {
	#region DLL Import
	[DllImport ("sfunityoffline")]
    private static extern void onInit(IntPtr context); 
	[DllImport ("sfunityoffline")]
	private static extern void onInit_listener(IntPtr context,string gameObject, string listener); 
	[DllImport ("sfunityoffline")]
    private static extern void onExit(IntPtr context); 
	[DllImport ("sfunityoffline")]
	private static extern void onExitWithUI(IntPtr context, string gameObject, string runtimeScriptMethod);
	[DllImport ("sfunityoffline")]
	private static extern void pay (IntPtr context, string id, string gameObject, string runtimeScriptMethod);
	[DllImport ("sfunityoffline")]
	private static extern int isPaid (IntPtr context, string id);
	[DllImport ("sfunityoffline")]
	private static extern void viewMoreGames(IntPtr context); 
	[DllImport ("sfunityoffline")]
	private static extern int isMusicEnabled(IntPtr context);
	[DllImport ("sfunityoffline")]
	private static extern void onPause(IntPtr context);
	[DllImport ("sfunityoffline")]
	private static extern void onResume(IntPtr context);
	[DllImport ("sfunityoffline")]
	private static extern void setPaid(IntPtr context, string id);
	[DllImport ("sfunityoffline")]
	private static extern void extend(IntPtr context, string data,string gameObject, string listener);
	#endregion

	#region Singleton
	const string GameObjectName = "YIjieDanjiManager";
	static public YIjieDanjiManager mInst = null;
    static public YIjieDanjiManager Instance(){
		var go = GameObject.Find(GameObjectName);
		if (go == null){
			go = new GameObject(GameObjectName);
		}

		YIjieDanjiManager comp = go.GetComponent<YIjieDanjiManager>();
		if(comp == null){
			comp = go.AddComponent<YIjieDanjiManager>() as YIjieDanjiManager;
		}	
		
		mInst = comp;
        return mInst;
    }
	#endregion	

	public void Init(){
		if (Application.platform == RuntimePlatform.Android){
			using (AndroidJavaClass unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer")) {
				using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {
					onInit (curActivity.GetRawObject());
				}
			}
		}
	}

	public int IsMusicEnabled(){
		using (AndroidJavaClass unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer")) {
			using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {
	            int ret = isMusicEnabled(curActivity.GetRawObject());
				return ret;
//						if (ret == 1) {
//							str = "isMusicEnabled =  TRUE";
//						} else {
//							str = "isMusicEnabled =  FALSE";
//						}
			}
		}
	}

	public void OnExitWithUI(){
		using (AndroidJavaClass unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer")) {
			using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))	 {
				onExitWithUI(curActivity.GetRawObject(), GameObjectName, "ExitResult");
			}
		}
	}

	// OnExitWithUI() 的回调函数
	public void ExitResult(string result){
		Debug.Log ("ExitResult:" + result);
		#if UNITY_EDITOR
		Debug.Log ("ExitResult ####### UNITY_EDITOR" );
		UnityEditor.EditorApplication.isPlaying = false ;
		#else
		Debug.Log ("ExitResult ####### Application.Quit " );
		Application.Quit ();
		#endif
	}

	wzlzIAPInterface _iapMng;
	string _productId;

	public void Pay(string productId, wzlzIAPInterface iapMng){
		_iapMng = iapMng;
		using (AndroidJavaClass unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer")) {
			using (AndroidJavaObject curActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {
        		pay(curActivity.GetRawObject(), productId, GameObjectName, "PayResult");
			}
		}
	}

	void PayResult(string result)
	{
		Debug.Log("------------PayResult="+result);
		SFJSONObject sfjson = new SFJSONObject(result);
		string type = (string)sfjson.get("result");
		string customParams = (string)sfjson.get("customParams");
		
		if (APaymentHelper.PayResult.SUCCESS == type) {
			_iapMng.SuccessBuy(_productId);
		} else if (APaymentHelper.PayResult.FAILURE == type) {
			_iapMng.FailBuy(_productId, result);
		}  else if (APaymentHelper.PayResult.CANCELED == type) {
			_iapMng.CancleBuy(_productId, result);
		}
	}
}
